package com.ts.tsjob.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ts.tsjob.model.AddyjMess;
import com.ts.tsjob.model.EmergencyMiddleFormationEntity;
import com.ts.tsjob.model.HttpUtil;
import com.ts.tsjob.model.Schedule;
import com.ts.tsjob.services.TSKaoQin;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dangan")
@Slf4j
public class danganController {

    @Autowired
    private TSKaoQin tsKaoQin;

    /**
     * 获取巡查档案数据
     *
     * @param pageSize
     * @param currentPage
     * @return
     */
    @GetMapping("/xuncha")
    public String xuncha(@RequestParam("pageSize") String pageSize, @RequestParam("currentPage") String currentPage) throws IOException, ParseException {
        log.info("来自接口：/dangan/xuncha 的参数：{}", pageSize + currentPage);
        String xuncha = tsKaoQin.xuncha(pageSize, currentPage);
        return xuncha;
    }

    @GetMapping("/work")
    public String work(@RequestParam("start") String start, @RequestParam("end") String end) throws IOException, ParseException {
        String work = tsKaoQin.work(start, end);
        return work;
    }

    @GetMapping("/chick")
    public String chick() throws IOException, ParseException {
        String chick = tsKaoQin.chick();
        return chick;
    }

    @GetMapping("/yanghu")
    public String yanghu() throws IOException, ParseException {
        String yanghu = tsKaoQin.yanghu();
        return yanghu;
    }

    @GetMapping("/shebei")
    public String shebei() throws IOException, ParseException {
        String shebei = tsKaoQin.shebei();
        return shebei;
    }

    @GetMapping("/guiji")
    public String guiji(@RequestParam(value = "currentPage", required = false) String currentPage,
                        @RequestParam("day") String day,
                        @RequestParam(required = false, value = "size") String size) throws IOException, ParseException {
        if (StringUtils.isEmpty(size)) {
            size = "20";
        }
        String guiji = tsKaoQin.guiji(currentPage, day, size);
        return guiji;
    }

    @GetMapping("/danwei")
    public String danwei() throws IOException, ParseException {
        String danwei = tsKaoQin.danwei();
        return danwei;
    }

    @GetMapping("/penpen")
    public String penpen(@RequestParam("id") String id) throws IOException, ParseException {
        String penpen = tsKaoQin.penpen(id);
        return penpen;
    }

    @PostMapping("/schedule")
    public String schedule(@RequestBody Schedule schedule) {
        log.info("来自接口：/dangan/scheduled 的参数：{}", schedule.toString());
        String message = "错误的参数！";
//        message = HttpsUtils.sendByHttp(JSON.parseObject(JSON.toJSONString(schedule)), "http://shslgc.com:3133/schedule");
        message = HttpUtil.doPost("http://shslgc.com:3133/schedule", JSON.toJSONString(schedule));
        return message;
    }


    @PostMapping("/seeyon/rest/SyoRestController/insertyj")
    public String insertyj(@RequestBody EmergencyMiddleFormationEntity entity) {
        log.info("来自接口：/seeyon/rest/SyoRestController/insertyj 的参数：{}", entity.toString());
        String message = "错误的参数！";
        String token = null;
        try {
            token = getToken2();
        } catch (Exception e) {
            log.info("token获取失败");
            return "token获取失败";
        }
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestEnvirController/insertyj", JSON.toJSONString(entity), token);
        return message;
    }

    @GetMapping("/seeyon/rest/SyoRestController/allxcsb")
    public String allxcsb() {
        String message = "错误的参数！";
        String token = null;
        try {
            token = getToken2();
        } catch (Exception e) {
            log.info("token获取失败");
            return "token获取失败";
        }
        message = HttpUtil.doGet("http://shslgc.com:8929/seeyon/rest/SyoRestEnvirController/allxcsb", new HashMap<>(), token);
        return message;
    }

    @PostMapping("/seeyon/rest/SyoRestController/getmessfkui")
    public String getmessfkui(@RequestBody Map<String, Object> params) {
        log.info("来自接口：/seeyon/rest/SyoRestController/getmessfkui 的参数：{}", params);
        String message = "错误的参数！";
        String token = null;
        try {
            token = getToken2();
        } catch (Exception e) {
            log.info("token获取失败");
            return "token获取失败";
        }
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestEnvirController/getmessfkui", JSON.toJSONString(params), token);
        return message;
    }


    @PostMapping("/seeyon/rest/SyoRestController/insertInspectRecords")
    public String insertInspectRecords(@RequestBody Map<String, Object> map) {
        log.info("来自接口：/seeyon/rest/SyoRestController/insertInspectRecords 的参数：{}", map);
        String message = "错误的参数！";
        String token = null;
        try {
            token = getToken2();
        } catch (Exception e) {
            log.info("token获取失败");
            return "token获取失败";
        }
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestEnvirController/insertInspectRecords", JSON.toJSONString(map), token);
        return message;
    }

    @PostMapping("/seeyon/rest/SyoRestController/uploadInspectFile")
    public String uploadInspectFile(@RequestBody List<Map<String, Object>> params) {
        log.info("来自接口：/seeyon/rest/SyoRestController/uploadInspectFile 的参数：{}", params);
        String message = "错误的参数！";
        String token = null;
        try {
            token = getToken2();
        } catch (Exception e) {
            log.info("token获取失败");
            return "token获取失败";
        }
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestEnvirController/uploadInspectFile", JSON.toJSONString(params), token);
        return message;
    }


    @PostMapping("/seeyon/rest/SyoRestController/insrtExcotion")
    public String insrtExcotion(@RequestBody Map<String, Object> params) {
        log.info("来自接口：/seeyon/rest/SyoRestController/uploadInspectFile 的参数：{}", params);
        String message = "错误的参数！";
        String token = null;
        try {
            token = getToken2();
        } catch (Exception e) {
            log.info("token获取失败");
            return "token获取失败";
        }
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestEnvirController/insrtExcotion", JSON.toJSONString(params), token);
        return message;
    }

    @GetMapping("/seeyon/rest/token/rest/58b17363-848e-43bf-a772-205b0415cbb2")
    public String token() {
        String message = "获取token异常！";
        message = HttpUtil.doGet("http://shslgc.com:8929/seeyon/rest/token/rest/58b17363-848e-43bf-a772-205b0415cbb2", new HashMap<>());
        return message;
    }

    @GetMapping("/seeyon/rest/token/rest01/22fe7a6c-a3c9-4638-ac35-08846a3a2d87")
    public String token1(@RequestParam("loginName") String loginName) {
        String message = "获取token异常！";
        HashMap<String, Object> params = new HashMap<>();
        params.put("loginName", loginName);
        message = HttpUtil.doGet("http://183.192.66.184/seeyon/rest/token/rest01/22fe7a6c-a3c9-4638-ac35-08846a3a2d87?loginName=oa1", new HashMap<>());
        return message;
    }

    @PostMapping("/addyjMess")
    public String addyjMess(@RequestBody List<AddyjMess> data) {
        log.info("来自接口：/addyjMess的参数：{}", data.toString());
        String message = "错误的参数！";
//        message = HttpsUtils.sendByHttp(JSON.parseObject(JSON.toJSONString(schedule)), "http://shslgc.com:3133/schedule");
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestController/addyjMess", JSON.toJSONString(data));
        return message;
    }

    @PostMapping("/seeyon/rest/SyoRestController/getEinvolceFieldInfo")
    public String getEinvolceFieldInfo() {
        log.info("来自接口：/getEinvolceFieldInfo的参数：{}");
        String message = "错误的参数！";
//        message = HttpsUtils.sendByHttp(JSON.parseObject(JSON.toJSONString(schedule)), "http://shslgc.com:3133/schedule");
        message = HttpUtil.doPost("http://shslgc.com:8929/seeyon/rest/SyoRestController/getEinvolceFieldInfo", "");
        return message;
    }

    @GetMapping("/v5/direction/driving")
    public String knowledge(@RequestParam(required = false) String origin,
                            @RequestParam(required = false) String destination,
                            @RequestParam(required = false) String key,
                            @RequestParam(required = false) String strategy,
                            @RequestParam(required = false) String show_fields) {
        log.info("来自接口：/v5/direction/driving 的参数：{}", origin);
        if (StringUtils.isEmpty(destination))
            destination = "121.203741,30.921598";
        if (StringUtils.isEmpty(key))
            key = "d30c3b98e5ad84b74dd0724e719ab3eb";
        if (StringUtils.isEmpty(strategy))
            strategy = "0";
        if (StringUtils.isEmpty(show_fields))
            show_fields = "cost";
        String message = "错误的参数！";
//        message = HttpsUtils.sendByHttp(JSON.parseObject(JSON.toJSONString(schedule)), "http://shslgc.com:3133/schedule");
        message = HttpUtil.doGet("https://restapi.amap.com/v5/direction/driving?origin=" + origin + "&destination=" + destination + "&key=" + key + "&strategy=" + strategy + "&show_fields=" + show_fields, new HashMap());
        return message;
    }


    private String getToken1() {
        String result = HttpUtil.doGet("http://shslgc.com:8929/seeyon/rest/token/rest/58b17363-848e-43bf-a772-205b0415cbb2", new HashMap<>());
        String token = null;
        try {
            JSONObject jsonObject = JSONObject.parseObject(result);
            token = jsonObject.getString("id");
        } catch (Exception e) {
            return result;
        }
        log.info("来自接口：/seeyon/rest/SyoRestController/getmessfkui 获取到token：{}", token);
        return token;
    }

    private String getToken2() {
        String result = HttpUtil.doGet("http://shslgc.com:8929/seeyon/rest/token/rest/58b17363-848e-43bf-a772-205b0415cbb2", new HashMap<>());
        String token = null;
        try {
            JSONObject jsonObject = JSONObject.parseObject(result);
            token = jsonObject.getString("id");
        } catch (Exception e) {
            return result;
        }
        log.info("来自接口：/seeyon/rest/SyoRestController/getmessfkui 获取到token：{}", token);
        return token;
    }

}
